#![allow(unused_variables)]
use sqlx::Error;
use super::CountNum;
use super::RecommendBean;
use super::RecommendQueryRequest;
use super::Table;

impl<'c> Table<'c, RecommendBean> {
pub async fn activate_recommend(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"update cs_recommends set active = true where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn add_recommend(&self,recommend:RecommendBean) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"insert into cs_recommends ( name,active,goods_type_id,goods_id) values (?,?,?,?)"#
		)
		.bind(recommend.name)
		.bind(recommend.active)
		.bind(recommend.goods_type_id)
		.bind(recommend.goods_id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn count_active_recommend_records(&self) ->Result<CountNum, Error> {
	let result = sqlx::query_as(
	  r#"select count(*) as countNum from cs_recommends where active = true"#
	)
	.fetch_one(&(&*self.pool).clone().unwrap())
	.await;
	return result;
}

pub async fn count_search_recommends_by_fields_records(&self,recommend_query_request:RecommendQueryRequest) ->Result<CountNum, Error> {
		let mut query = r#"select count(*) as countNum from cs_recommends where 1=1 "#.to_string();
		let name = recommend_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = recommend_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let goods_type_id = recommend_query_request.goods_type_id.unwrap_or_default();
		if !goods_type_id.is_empty() {
			let query_goods_type_id  = format!("{}{}{}"," and goods_type_id = ",goods_type_id," ");
			query = format!("{} {}", query, query_goods_type_id);
		}
		let goods_id = recommend_query_request.goods_id.unwrap_or_default();
		if !goods_id.is_empty() {
			let query_goods_id  = format!("{}{}{}"," and goods_id = ",goods_id," ");
			query = format!("{} {}", query, query_goods_id);
		}
		let result = sqlx::query_as(&query)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn delete_recommend(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"delete from cs_recommends where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn find_recommend_by_id(&self,id:i64) ->Result<RecommendBean, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active,goods_type_id,goods_id from cs_recommends where id = ? "#
		)
		.bind(id)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn find_recommend_by_name(&self,name:String) ->Result<RecommendBean, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active,goods_type_id,goods_id from cs_recommends where name = ?;"#
		)
		.bind(name)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn list_active_recommends(&self) ->Result<Vec<RecommendBean>, Error> {
		let result = sqlx::query_as(
	r#"select id,name,active,goods_type_id,goods_id from cs_recommends where active = true order by name asc"#
		)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn list_all_recommends(&self) ->Result<Vec<RecommendBean>, Error> {
		let result = sqlx::query_as(
	r#"select id,name,active,goods_type_id,goods_id from cs_recommends"#
		)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn search_recommends_by_fields(&self,recommend_query_request:RecommendQueryRequest) ->Result<Vec<RecommendBean>, Error> {
		let mut query = r#"select id,name,active,goods_type_id,goods_id from cs_recommends where 1=1 "#.to_string();
		let name = recommend_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = recommend_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let goods_type_id = recommend_query_request.goods_type_id.unwrap_or_default();
		if !goods_type_id.is_empty() {
			let query_goods_type_id  = format!("{}{}{}"," and goods_type_id = ",goods_type_id," ");
			query = format!("{} {}", query, query_goods_type_id);
		}
		let goods_id = recommend_query_request.goods_id.unwrap_or_default();
		if !goods_id.is_empty() {
			let query_goods_id  = format!("{}{}{}"," and goods_id = ",goods_id," ");
			query = format!("{} {}", query, query_goods_id);
		}
		let result = sqlx::query_as(&query)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn search_recommends_by_fields_by_page(&self,recommend_query_request:RecommendQueryRequest) ->Result<Vec<RecommendBean>, Error> {
		let mut query = r#"select id,name,active,goods_type_id,goods_id from cs_recommends where 1=1 "#.to_string();
		let name = recommend_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = recommend_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let goods_type_id = recommend_query_request.goods_type_id.unwrap_or_default();
		if !goods_type_id.is_empty() {
			let query_goods_type_id  = format!("{}{}{}"," and goods_type_id = ",goods_type_id," ");
			query = format!("{} {}", query, query_goods_type_id);
		}
		let goods_id = recommend_query_request.goods_id.unwrap_or_default();
		if !goods_id.is_empty() {
			let query_goods_id  = format!("{}{}{}"," and goods_id = ",goods_id," ");
			query = format!("{} {}", query, query_goods_id);
		}
		let pagenum =  recommend_query_request.page.unwrap_or_default();
		let pagesize =  recommend_query_request.rows.unwrap_or_default();
		let start = (pagenum - 1) * pagesize;
		let limit = pagesize;
		query = format!("{} limit {} offset {} ", query, limit, start);
		let result = sqlx::query_as(&query)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn soft_delete_recommend(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"update cs_recommends set active = false where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn toggle_recommend(&self,id:i64) ->Result<u64, Error> {
	let result = sqlx::query(
	  r#"update cs_recommends set active = not active where id = ? "#
	)
	.bind(id)
	.execute(&(&*self.pool).clone().unwrap())
	.await;
	match result {
		Ok(result) => {
			Ok(result.rows_affected())
		},
		Err(err) => {
			Err(err)
		}
	}
}

pub async fn update_recommend(&self,recommend:RecommendBean) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"update cs_recommends set name = ? ,active = ? ,goods_type_id = ? ,goods_id = ? where id = ? "#
		)
		.bind(recommend.name)
		.bind(recommend.active)
		.bind(recommend.goods_type_id)
		.bind(recommend.goods_id)
		.bind(recommend.id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn find_recommend_indexed_name(&self,short_name:String) ->Result<Vec<RecommendBean>, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active,goods_type_id,goods_id from cs_recommends where name like concat(? ,'%') "#
	)
	.bind(short_name)
	.fetch_all(&(&*self.pool).clone().unwrap())
	.await;
	return result;
}

pub async fn add_recommend_type(&self,goods_id:i64,recommend_type_id:i64) ->Result<u64, Error> {
	let result = sqlx::query(
	   r#"insert into cs_recommends ( name,active,goods_type_id,goods_id) values (?,?,?,?)"#
	)
	.bind(format!("Recommend:{}:{}:Add",goods_id,recommend_type_id))
	.bind(true)
	.bind(recommend_type_id)
	.bind(goods_id)
	.execute(&(&*self.pool).clone().unwrap())
	.await;
	match result {
		Ok(result) => {
			Ok(result.rows_affected())
		},
		Err(err) => {
			Err(err)
		}
	}
}

pub async fn remove_recommend_type(&self,goods_id:i64,recommend_type_id:i64) ->Result<u64, Error> {
	let result = sqlx::query(
	   r#"delete from cs_recommends where goods_id = ? and goods_type_id = ?"#
	)
	.bind(goods_id)
	.bind(recommend_type_id)
	.execute(&(&*self.pool).clone().unwrap())
	.await;
	match result {
		Ok(result) => {
			Ok(result.rows_affected())
		},
		Err(err) => {
			Err(err)
		}
	}
}

pub async fn is_recommend(&self, goods_id:i64,recommend_type_id:i64) ->Result<CountNum, Error> {
    let result = sqlx::query_as(
	  r#"select count(*) as countNum from cs_recommends where goods_type_id=? and goods_id=? "#
	)
    .bind(recommend_type_id)
    .bind(goods_id)
	.fetch_one(&(&*self.pool).clone().unwrap())
	.await;
    return result;
}

}
